package car.aistar.controller;

import car.aistar.dto.Result;
import car.aistar.entity.*;
import car.aistar.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName InforController
 * @Description TODO
 * @Author 肖
 * @Date 2020/10/2 16:27
 * Version 1.0
 **/
@Controller
                @RequestMapping("/cardetail")
public class InforController {
    @Autowired
    private CarDetailService carDetailService;
    @Autowired
    private SellerService sellerService;
    @Autowired
    private CarBrandsService carBrandsService;
    @Autowired
    private CarStyleService carStyleService;
    @Autowired
    private PictureService pictureService;

    @Autowired
    private CarCartService carCartService;
    @Autowired
    private SellReserveService sellReserveService;
    @Autowired
    private ReserveService reserveService;


    /**
    *@Author 肖相荣
    *@Description //TODO 车辆详情页的数据展示（根据车辆ID进行取值）
    *@Date 22:06 2020/10/8
    *@Param [model, carDetail]
    *@return java.lang.String
    **/
    @RequestMapping("/infor")
    public String infor(Model model, CarDetail carDetail){
        CarDetail carDetail1 = carDetailService.queryById(carDetail.getId());

        CarBrands carBrands = carBrandsService.queryById(carDetail1.getBrId());

        CarStyle carStyle = carStyleService.queryById(carDetail1
                .getStId());

//       Seller seller = sellerService.queryById(Long.valueOf(carDetail1.getSalId()));
       Seller seller = sellerService.queryById(3L);

        Picture  picture = pictureService.queryById(carDetail.getId());


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date1=sdf.format(carDetail1.getCInsure());
        model.addAttribute("insure",date1);

        model.addAttribute("phone",picture);
        model.addAttribute("brand",carBrands);
        model.addAttribute("style",carStyle);
        model.addAttribute("infor",carDetail1);
        model.addAttribute("seller",seller);

        return "carDesc/infor";
    }

    /**
    *@Author 肖相荣
    *@Description //TODO 车辆详情页砍价表的插入
    *@Date 22:07 2020/10/8
    *@Param [carCart]
    *@return car.aistar.dto.Result
    **/
    @GetMapping("/kanjia")
    public @ResponseBody Result kanjia(CarCart carCart){

       carCartService.insert(carCart);
       return new Result("200","success",carCart);

    }
    /**
    *@Author 肖相荣
    *@Description //TODO 车辆详情页预约表的操作
    *@Date 22:07 2020/10/8
    *@Param [id, resTime]
    *@return car.aistar.dto.Result
    **/
    @GetMapping("/yuyue")
    public @ResponseBody Result kanjia(String uId,String cId,String selId,String resTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

       Reserve reserve = new Reserve();
       reserve.setUId(Long.valueOf(uId));
       reserve.setSId(Long.valueOf(selId));
       reserve.setResCarId(Long.valueOf(cId));
        try {
            reserve.setResTime(sdf.parse(resTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        reserve.setResStatus(0L);
        reserveService.insert(reserve);
        return new Result("200","success",reserve);
    }



    @Autowired
    private  UserCollectService userCollectService;
    @GetMapping("/collect")
    public @ResponseBody Result collect(String uId,String cId,String addTime){

        CarDetail carDetail = carDetailService.queryById(Long.valueOf(cId));

        UserCollect userCollect = new UserCollect();
        userCollect.setUId(Long.valueOf(uId));
        userCollect.setCId(Long.valueOf(cId));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            userCollect.setAddTime(sdf.parse(addTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        userCollect.setCPhoto(carDetail.getCPhoto());
        System.out.println(userCollect);
        userCollectService.insert(userCollect);
        return new Result("200","success","");
    }


}
